async function process_tasks() {
let campaignsRef = db.collection('campaigns')
let activeRef = await campaignsRef.where('active', '==', true).select().get();
for (campaign of activeRef.docs) {
console.log(campaign.id);
(CHOICE 1) let tasksRef = await campaignsRef.doc(campaign.id).collection('tasks').get();
(CHOICE 2) let tasksRef = await campaign.collection('tasks').get();
for(task of tasksRef.docs) {
console.log(task.id, task.data())
}
}
}
When I use (CHOICE 2) , "documentRef" is not a valid DocumentReference ERROR show.
There is no variable named documentRef in my code( as a note to those comment who wrongly think so)
I wonder why? shouldnt campaign equal campaignsRef.doc(campaign.id)?
Everything you wrote in the second election is correct, except for one thing. The variable (campaign) created in the for loop is DocumentSnapshot. You have to use .ref to access reference from DocumentSnapshot.
async function process_tasks() {
let campaignsRef = db.collection('campaigns')
let activeRef = await campaignsRef.where('active', '==', true).select().get();
for (campaign of activeRef.docs) {
//campaign is DocumentSnapshot
console.log(campaign.id);
(CHOICE 1) let tasksRef = await campaignsRef.doc(campaign.id).collection('tasks').get();
(CHOICE 2) let tasksRef = await campaign.ref.collection('tasks').get(); // add .ref after campaign
for(task of tasksRef.docs) {
console.log(task.id, task.data())
}
}
}
Thanks.